

//
//  CollectionHelper.m
//  cookBook
//
//  Created by xalo on 16/1/25.
//  Copyright © 2016年 张广斌. All rights reserved.
//

#import "CollectionHelper.h"
#import "Model.h"
#import <FMDB/FMDB.h>

static CollectionHelper *handle = nil;

@interface CollectionHelper ()
@property (nonatomic,strong)FMDatabase *db;

@end


@implementation CollectionHelper

+ (CollectionHelper *)sharedManager
{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        handle = [[CollectionHelper alloc] init];
    });
    return handle;
}

-(instancetype)init
{
    self = [super init];
    if (self) {
        
        NSString *documentPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0];
        NSString *dbPath = [documentPath stringByAppendingPathComponent:@"collection.sqlite"];
        
        
        self.db = [FMDatabase databaseWithPath:dbPath];
        
        //打开数据库
        [self.db open];
        
        //创建表
        if ([self.db open]) {
            
            //sql 语句
            NSString *sql = @"create table 'collection' (sid integer primary key autoincrement not null,model blob,ID text);";
            //创建表
            BOOL result = [self.db executeUpdate:sql];
            
            if (result) {
                NSLog(@"表创建成功");
            }
  
        }
        
        //关闭数据库
        [self.db close];
        
    }
    return self;
}

//归档
+ (NSData *)archiverWithObject:(id)object key:(NSString *)key
{
    NSMutableData *data = [NSMutableData data];
    
    NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData:data];
    
    [archiver encodeObject:object forKey:key];
    
    [archiver finishEncoding];
    
    return data;
}

//反归档
+ (id)unarchiverWithData:(NSData *)data key:(NSString *)key
{
    NSKeyedUnarchiver *unarchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:data];
    
    id object = [unarchiver decodeObjectForKey:key];
    
    [unarchiver finishDecoding];
    
    return object;
    
}



//添加到数据库
- (BOOL)insertToSqlitWithModel:(Model *)model ID:(NSString *)ID
{

    //归档
    NSData *data = [CollectionHelper archiverWithObject:model key:ID];
    
    //打开数据库
    [self.db open];
    
    //sql语句
    NSString *sql = @"insert into 'collection' (model,ID) values(?,?);";
    
    //执行
    BOOL result = [self.db executeUpdate:sql,data,ID];
    
    //关闭数据库
    [self.db close];

    return result;
}

//根据ID删除
- (BOOL)deleteWithID:(NSString *)ID
{
    
    //sql语句
    NSString *sql = @"delete from 'collection' where ID = ?;";
    
    //打开数据库
    [self.db open];
    
    //执行
    BOOL result = [self.db executeUpdate:sql,ID];
    
    //关闭数据库
    [self.db close];
    
    return result;
}

//全部删除
- (BOOL)deleteAll
{
    //sql 语句
    NSString *sql = @"delete from 'collection';";
    
    //打开数据库
    [self.db open];
    
    //执行
    BOOL result = [self.db executeUpdate:sql];
    
    //关闭数据库
    [self.db close];
    
    return result;
}


//根据ID查找
- (Model *)selectWithID:(NSString *)ID
{
    NSString *sql = [NSString stringWithFormat:@"select *from 'collection' where ID = '%@';",ID];
    
    //打开数据库
    [self.db open];
    
    //执行查找方法
    FMResultSet *fr = [self.db executeQuery:sql];
    
    //创建model
    Model *model = [[Model alloc] init];
    
    while ([fr next]) {
        NSData *data = [fr dataForColumnIndex:1];
        NSString *ID = [fr stringForColumnIndex:2];
        
        //反归档
        model = [CollectionHelper unarchiverWithData:data key:ID];
    }
    
    [fr close];
    
    //关闭数据库
    [self.db close];
    
    return model;
}


//查找全部
- (NSArray *)selectAll
{
    NSMutableArray *array = [NSMutableArray array];
    
    NSString *sql = @"select *from 'collection';";
    
    //打开数据库
    [self.db open];
    
    FMResultSet *fr = [self.db executeQuery:sql];
    
    while ([fr next]) {
        NSData *data = [fr dataForColumnIndex:1];
        NSString *ID = [fr stringForColumnIndex:2];
        
        //反归档
        Model *model = [CollectionHelper unarchiverWithData:data key:ID];
        
        [array addObject:model];
    }
    
    [fr next];
    
    //关闭数据库
    [self.db close];
    
    return array;
}







@end
